/*
 * Copyright 2017-2101 Innel.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.hykj.xxltest.controller;

import com.github.pagehelper.PageInfo;
import com.hykj.xxltest.base.AppBack;
import com.hykj.xxltest.base.RequiresRole;
import com.hykj.xxltest.model.PayOrderModel;
import com.hykj.xxltest.service.PayOrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * 支付订单凭据
 *
 * @author innel
 * @email 2638086622@qq.com
 * @date 2018-04-05 13:13:09//
 */
@Controller
@RequestMapping("/payorder")
@Api(description = "支付订单凭据")
public class PayOrderController {

    private static Logger logger = Logger.getLogger(PayOrderController.class);

    @Autowired
    private PayOrderService payOrderService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @ResponseBody
    @RequiresRole(role = "1")
    @ApiOperation(
            value = "获取列表",
            notes = "备注：",
            httpMethod = "POST")
    public AppBack list(@ApiParam(value = "每页显示个数,不填默认10")
                        @RequestParam(required = false)
                                Integer pageSize,
                        @ApiParam(value = "")
                        @RequestParam(required = false)
                                Integer id,
                        @ApiParam(value = "用户ID")
                        @RequestParam(required = false)
                                Integer userId,
                        @ApiParam(value = "0未付款 1已付款 2取消支付 ")
                        @RequestParam(required = false)
                                Integer status,
                        @ApiParam(value = "支付类型 1支付宝 2微信 3余额（暂未开发）")
                        @RequestParam(required = false)
                                Integer payType,
                        @ApiParam(value = "订单类型 1购买课程 2购买积分商城(这张表不保存，在order表里保存 以后是通用) 3未开发")
                        @RequestParam(required = false)
                                Integer orderType,
                        @ApiParam(value = "目标ID")
                        @RequestParam(required = false)
                                Integer targetId,
                        @ApiParam(value = "token")
                        @RequestParam(required = false)
                                String token,
                        @ApiParam(value = "开始时间 范例 2017-11-06 16:02:11", hidden = true)
                        @RequestParam(required = false)
                                String startTime,
                        @ApiParam(value = "截止时间 格式 YYYYMMDD 也可以", hidden = true)
                        @RequestParam(required = false)
                                String endTime,
                        @ApiParam(value = "搜索关键字")
                        @RequestParam(required = false)
                                String searchKey,
                        @ApiParam(value = "是否是后台列表请求", hidden = true)
                        @RequestParam(required = false)
                                String backList,
                        @ApiParam(value = "是否简单显示", hidden = true)
                        @RequestParam(required = false)
                                String noD,
                        @ApiParam(value = "不填不分页")
                        @RequestParam(required = false)
                                Integer page) {
        Map<String, Object> params = new HashMap<>();
        params.put("pageSize", pageSize);
        params.put("id", id);
        params.put("userId", userId);
        params.put("status", status);
        params.put("payType", payType);
        params.put("orderType", orderType);
        params.put("targetId", targetId);
        params.put("startTime", startTime);
        params.put("endTime", endTime);
        params.put("searchKey", searchKey);
        params.put("noD", noD);
        params.put("backList", backList);
        if (page == null) {
            return new AppBack(payOrderService.getDList(params));
        }
        PageInfo<PayOrderModel> p = payOrderService.getDList(params, page);
        return new AppBack(p);
    }


    /**
     * 信息
     */
    @RequestMapping("/info")
    @ResponseBody
    @RequiresRole(role = "1")
    @ApiOperation(
            value = "获取当前资源",
            notes = "备注：",
            httpMethod = "POST")
    public AppBack info(@ApiParam(value = "用户ID")
                        @RequestParam(required = false)
                                Integer userId,
                        @ApiParam(value = "token")
                        @RequestParam(required = false)
                                String token,

                        @ApiParam(value = "当前资源的ID", required = true)
                        @RequestParam Integer id) {
        PayOrderModel payOrder = payOrderService.getByIdD(id);
        if (payOrder == null) {
            return new AppBack(-1, "当前资源不存在");
        }
        return new AppBack(payOrder);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @ResponseBody
    @RequiresRole(role = "1")
    @ApiOperation(
            value = "保存当前资源",
            notes = "备注：",
            httpMethod = "POST")
    public AppBack save(@ApiParam(value = "token")
                        @RequestParam(required = false)
                                String token,
                        @ApiParam(value = "用户ID", required = true)
                        @RequestParam
                                Integer userId,
                        @ApiParam(value = "订单金额", required = true)
                        @RequestParam
                                BigDecimal price,
                        @ApiParam(value = "0未付款 1已付款 2取消支付 ", required = true)
                        @RequestParam
                                Integer status,
                        @ApiParam(value = "收到支付的时间", required = true)
                        @RequestParam
                                Date payTime,
                        @ApiParam(value = "支付类型 1支付宝 2微信 3余额（暂未开发）", required = true)
                        @RequestParam
                                Integer payType,
                        @ApiParam(value = "订单号", required = true)
                        @RequestParam
                                String orderCode,
                        @ApiParam(value = "订单类型 1购买课程 2购买积分商城(这张表不保存，在order表里保存 以后是通用) 3未开发", required = true)
                        @RequestParam
                                Integer orderType,
                        @ApiParam(value = "目标ID", required = true)
                        @RequestParam
                                Integer targetId,
                        @ApiParam(value = "选填参数控制 默认不填", hidden = true)
                        @RequestParam(required = false)
                                Integer extra) {
        PayOrderModel payOrder = new PayOrderModel();
        payOrder.setUserId(userId);
        payOrder.setPrice(price);
        payOrder.setStatus(status);
        payOrder.setPayTime(payTime);
        payOrder.setPayType(payType);
        payOrder.setOrderCode(orderCode);
        payOrder.setOrderType(orderType);
        payOrder.setTargetId(targetId);

        payOrderService.create(payOrder);
        return new AppBack();

    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @ResponseBody
    @RequiresRole(role = "1")
    @ApiOperation(
            value = "更新当前资源",
            notes = "备注：",
            httpMethod = "POST")
    public AppBack update(@ApiParam(value = "token")
                          @RequestParam(required = false)
                                  String token,
                          @ApiParam(value = "资源ID")
                          @RequestParam

                                  Integer id,

                          @ApiParam(value = "用户ID")
                          @RequestParam(required = false)

                                  Integer userId,
                          @ApiParam(value = "订单金额")
                          @RequestParam(required = false)

                                  BigDecimal price,
                          @ApiParam(value = "0未付款 1已付款 2取消支付 ")
                          @RequestParam(required = false)

                                  Integer status,
                          @ApiParam(value = "收到支付的时间")
                          @RequestParam(required = false)

                                  Date payTime,
                          @ApiParam(value = "支付类型 1支付宝 2微信 3余额（暂未开发）")
                          @RequestParam(required = false)

                                  Integer payType,
                          @ApiParam(value = "订单号")
                          @RequestParam(required = false)

                                  String orderCode,
                          @ApiParam(value = "订单类型 1购买课程 2购买积分商城(这张表不保存，在order表里保存 以后是通用) 3未开发")
                          @RequestParam(required = false)

                                  Integer orderType,
                          @ApiParam(value = "目标ID")
                          @RequestParam(required = false)

                                  Integer targetId,
                          @ApiParam(value = "选填参数控制 默认不填")
                          @RequestParam(required = false)
                                  Integer searchKey) {
        PayOrderModel payOrder = new PayOrderModel();
        payOrder.setId(id);
        payOrder.setUserId(userId);
        payOrder.setPrice(price);
        payOrder.setStatus(status);
        payOrder.setPayTime(payTime);
        payOrder.setPayType(payType);
        payOrder.setOrderCode(orderCode);
        payOrder.setOrderType(orderType);
        payOrder.setTargetId(targetId);
        payOrderService.update(payOrder);
        return new AppBack();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @ResponseBody
    @RequiresRole(role = "1")
    @ApiOperation(
            value = "删除当前资源",
            notes = "备注：",
            httpMethod = "POST")
    public AppBack delete(@ApiParam(value = "用户ID")
                          @RequestParam(required = false)
                                  Integer userId,
                          @ApiParam(value = "token")
                          @RequestParam(required = false)
                                  String token,
                          @ApiParam(value = "资源ID", required = true)
                          @RequestParam
                                  int id) {

        PayOrderModel newPayOrder = new PayOrderModel();
        newPayOrder.setStatus(-10);
        newPayOrder.setId(id);
        payOrderService.update(newPayOrder);
        return new AppBack();
    }

}
